home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Hello / Sources / HelloPrt.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  12.9 KB  |  431 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                HelloPrt.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Hello.hpp"
  11.  
  12. #ifndef HELLOPRT_H
  13. #include "HelloPrt.h"
  14. #endif
  15.  
  16. #ifndef HELLOFRM_H
  17. #include "HelloFrm.h"
  18. #endif
  19.  
  20. #ifndef HELLOSEL_H
  21. #include "HelloSel.h"
  22. #endif
  23.  
  24. // ----- Framework Layer -----
  25.  
  26. #ifndef FWUTIL_H
  27. #include "FWUtil.h"
  28. #endif
  29.  
  30. #ifndef FWITERS_H
  31. #include "FWIters.h"
  32. #endif
  33.  
  34. #ifndef FWPRESEN_H
  35. #include "FWPresen.h"
  36. #endif
  37.  
  38. // ----- OS Layer -----
  39.  
  40. #ifndef FWMENU_H
  41. #include "FWMenu.h"
  42. #endif
  43.  
  44. #ifndef FWCFMRES_H
  45. #include "FWCFMRes.h"
  46. #endif
  47.  
  48. #ifndef FWRESTYP_H
  49. #include "FWResTyp.h"
  50. #endif
  51.  
  52. #ifndef FWSUSINK_H
  53. #include "FWSUSink.h"
  54. #endif
  55.  
  56. #ifndef FWEVENT_H
  57. #include "FWEvent.h"
  58. #endif
  59.  
  60. #ifndef FWBARRAY_H
  61. #include "FWBArray.h"
  62. #endif
  63.  
  64. // ----- Foundation Includes -----
  65.  
  66. #ifndef FWSTREAM_H
  67. #include "FWStream.h"
  68. #endif
  69.  
  70. #ifndef FWSTRS_H
  71. #include "FWStrs.h"
  72. #endif
  73.  
  74. #ifndef FWSTRGAR_H
  75. #include "FWStrgAr.h"
  76. #endif
  77.  
  78. #ifndef FWSUSINK_H
  79. #include "FWSUSink.h"
  80. #endif
  81.  
  82. // ----- OpenDoc Includes -----
  83.  
  84. #ifndef SOM_Module_OpenDoc_StdProps_defined
  85. #include <StdProps.xh>
  86. #endif
  87.  
  88. #ifndef SOM_ODTranslation_xh
  89. #include <Translt.xh>
  90. #endif
  91.  
  92. #ifndef SOM_ODSession_xh
  93. #include <ODSessn.xh>
  94. #endif
  95.  
  96. //========================================================================================
  97. // Constants and Globals
  98. //========================================================================================
  99.  
  100. const ODValueType CHelloPart::kPartKind = kODFHelloKind;
  101. const ODValueType CHelloPart::kPartUserName = kODFHelloEditorUserString;
  102.  
  103. const ODValueType kHelloPartSetting = "Apple:ODFExamples:Setting:ODFHello";
  104.  
  105. const ODCommandID cFirstCommand        = FW_kFirstUserCommandID;
  106. const ODCommandID cSecondCommand    = FW_kFirstUserCommandID + 1;
  107.  
  108. #define kMainPresentation    "ODFExamples:Presentation:ODFHello"
  109.  
  110. //========================================================================================
  111. //    Runtime info
  112. //========================================================================================
  113.  
  114. #ifdef FW_BUILD_MAC
  115. #pragma segment odfhello
  116. #endif
  117.  
  118. //========================================================================================
  119. //    CHelloPart class
  120. //========================================================================================
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // CHelloPart constructor
  124. //----------------------------------------------------------------------------------------
  125.  
  126. CHelloPart::CHelloPart(ODPart* odPart) :
  127.     FW_CPart(odPart, CHelloPart::kPartKind, CHelloPart::kPartUserName, FW_gInstance, kPartIconID),
  128.     fTextData1(""),
  129.     fTextData2(""),
  130.     fIsFirstString(TRUE),
  131.     fPresentation(NULL)
  132. {
  133. }
  134.  
  135. //----------------------------------------------------------------------------------------
  136. // CHelloPart destructor
  137. //----------------------------------------------------------------------------------------
  138.  
  139. CHelloPart::~CHelloPart()
  140. {
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // CHelloPart::Initialize
  145. //----------------------------------------------------------------------------------------
  146.  
  147. void CHelloPart::Initialize(Environment* ev)    // Override
  148. {
  149.     FW_CPart::Initialize(ev);
  150.  
  151.     // ----- Register our Presentation
  152.     fPresentation = RegisterPresentation(ev, kMainPresentation, TRUE, 
  153.                                          new CHelloSelection(ev, this));
  154.  
  155.     // ----- Initialize the Archiver component -----
  156.     FW_InitializeArchiving(FW_gInstance);
  157.  
  158.     // ----- Open the resource file for this shared library -----
  159.     FW_CSharedLibraryResourceFile resFile;
  160.  
  161.     // ----- Create the Hello menu -----
  162.     FW_CPullDownMenu* pullDownMenu = new FW_CPullDownMenu(ev, resFile, kHelloPartStrings, kHelloMenuString);
  163.  
  164.     // ----- Add the Hello menu items -----
  165.     pullDownMenu->AppendTextItem(ev, resFile, kHelloPartStrings, kFirstMenuString, cFirstCommand, '1');
  166.     pullDownMenu->AppendTextItem(ev, resFile, kHelloPartStrings, kSecondMenuString, cSecondCommand, '2');
  167.  
  168.     // ----- Add the Hello menu to the menu bar -----
  169.     GetMenuBar(ev)->AdoptMenuLast(ev, pullDownMenu);
  170.  
  171.     // ----- Initialize the text data strings -----
  172.     FW_CString32 platformString;
  173.     ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kFirstString, fTextData1);
  174.     ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kSecondString, fTextData2);
  175.     ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kPlatformString, platformString);
  176.     fTextData2 += platformString;
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // CHelloPart::InternalizeContent
  181. //------------------------------------------------------------------------------
  182.  
  183. void CHelloPart::InternalizeContent(Environment* ev,
  184.                                     ODStorageUnit* storageUnit, 
  185.                                     FW_CCloneInfo* cloneInfo)    // Override
  186. {
  187.     FW_UNUSED(cloneInfo);
  188.  
  189.     if (FW_SUExistsThenFocus(ev, storageUnit, kODPropContents, CHelloPart::kPartKind))
  190.     {
  191.         if (storageUnit->GetSize(ev) != 0)
  192.         {
  193.             // Read the text data strings from storage
  194.             FW_CStorageUnitSink suSink(storageUnit, kODPropContents, CHelloPart::kPartKind);
  195.             FW_CReadableStream archive(&suSink);
  196.             
  197.             unsigned long stringCount;
  198.             archive >> stringCount;
  199.             if (stringCount > 0)
  200.                 FW_CStringArchiver::Read(archive, fTextData1);
  201.             if (stringCount > 1)
  202.                 FW_CStringArchiver::Read(archive, fTextData2);
  203.         }
  204.         if (storageUnit->Exists(ev, kODPropContents, kHelloPartSetting, 0))
  205.         {
  206.             // Read the part's setting from storage
  207.             storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kHelloPartSetting, 0, kODPosUndefined);
  208.             FW_CByteArray byteArray;
  209.             storageUnit->GetValue(ev, sizeof(FW_Boolean), byteArray);
  210.             byteArray.CopyBuffer(&fIsFirstString, sizeof(FW_Boolean));
  211.         }
  212.     }
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // CHelloPart::ExternalizeContent
  217. //----------------------------------------------------------------------------------------
  218.  
  219. void CHelloPart::ExternalizeContent(Environment* ev,
  220.                                     ODStorageUnit* storageUnit,
  221.                                     FW_CCloneInfo* cloneInfo)    // Override
  222. {
  223.     FW_UNUSED(cloneInfo);
  224.  
  225.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, CHelloPart::kPartKind, 0, kODPosUndefined);
  226.     storageUnit->Remove(ev);
  227.     storageUnit->AddValue(ev, CHelloPart::kPartKind);
  228.  
  229.     // Write the two text data strings to storage
  230.     FW_CStorageUnitSink suSink(storageUnit, kODPropContents, CHelloPart::kPartKind);
  231.     FW_CWritableStream archive(&suSink);
  232.     unsigned long stringCount = 2;
  233.  
  234.     archive << stringCount;
  235.     FW_CStringArchiver::Write(archive, fTextData1);
  236.     FW_CStringArchiver::Write(archive, fTextData2);
  237.  
  238.     // Write the part's setting to storage
  239.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kHelloPartSetting, 0, kODPosUndefined);
  240.     storageUnit->Remove(ev);
  241.     storageUnit->AddValue(ev, kHelloPartSetting);
  242.  
  243.     FW_CByteArray byteArray(&fIsFirstString, sizeof(FW_Boolean));
  244.     storageUnit->SetValue(ev, byteArray);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. // CHelloPart::AddProperties
  249. //----------------------------------------------------------------------------------------
  250.  
  251. void CHelloPart::AddProperties(Environment *ev, ODStorageUnit* storageUnit)    // Override
  252. {
  253.     FW_CPart::AddProperties(ev, storageUnit);
  254.  
  255.     //    FW_SUAddPropValue correctly tests if the property and value exist before
  256.     //    trying to create them
  257.     FW_SUAddPropValue(ev, storageUnit, kODPropContents, kHelloPartSetting);
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. // CHelloPart::NewFrame
  262. //----------------------------------------------------------------------------------------
  263.  
  264. FW_CFrame* CHelloPart::NewFrame(Environment* ev,
  265.                                 ODFrame* odFrame,
  266.                                 FW_CPresentation* presentation,
  267.                                 FW_Boolean fromStorage)    // Override
  268. {
  269.     FW_UNUSED(fromStorage);
  270.  
  271.     return new CHelloFrame(ev, odFrame, presentation, this);
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. // CHelloPart::DoMenu
  276. //----------------------------------------------------------------------------------------
  277.  
  278. FW_Boolean CHelloPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent) // Override
  279. {
  280.     FW_Boolean menuHandled = FALSE;
  281.     ODCommandID id = theMenuEvent.GetCommandID(ev);
  282.     
  283.     switch (id)
  284.     {
  285.         case cFirstCommand:
  286.         case cSecondCommand:
  287.             this->DoHelloCommand(ev, id);
  288.             menuHandled = TRUE;
  289.             break;
  290.  
  291.         default:
  292.             menuHandled = FALSE;
  293.     }
  294.     
  295.     return menuHandled;
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // CHelloPart::DoAdjustMenus
  300. //----------------------------------------------------------------------------------------
  301.  
  302. FW_Boolean CHelloPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, 
  303.                                      FW_Boolean hasMenuFocus,
  304.                                      FW_Boolean isRoot)    // Override
  305. {
  306.     if (hasMenuFocus)
  307.     {
  308.         // Set up the Hello menu
  309.         menuBar->EnableCommand(ev, cFirstCommand, !fIsFirstString);
  310.         menuBar->EnableCommand(ev, cSecondCommand, fIsFirstString);
  311.     }
  312.     
  313.     return FALSE;
  314. }
  315.  
  316. //----------------------------------------------------------------------------------------
  317. // CHelloPart::DoHelloCommand
  318. //----------------------------------------------------------------------------------------
  319.  
  320. void CHelloPart::DoHelloCommand(Environment* ev, ODCommandID id)
  321. {
  322.     // Toggle the flag that tells which string to display
  323.     fIsFirstString = !fIsFirstString;
  324.  
  325.     // Notify the proper authorities
  326.     this->PartChanged(ev);
  327. }
  328.  
  329. //----------------------------------------------------------------------------------------
  330. // CHelloPart::PartChanged
  331. //----------------------------------------------------------------------------------------
  332.  
  333. void CHelloPart::PartChanged(Environment* ev)
  334. {
  335.     // Mark the document's draft as changed so it can be saved
  336.     this->Changed(ev);
  337.  
  338.     // Mark the display frame as changed, so that containing parts can update links
  339.     FW_CPresentationFrameIterator piter(fPresentation);
  340.     this->ContentUpdated(ev, piter.First(), kODUnknownUpdate);
  341.  
  342.     // Force all display frames to be redrawn
  343.     fPresentation->Invalidate(ev);
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // CHelloPart::GetResourceString
  348. //----------------------------------------------------------------------------------------
  349.  
  350. void CHelloPart::GetResourceString(FW_ResourceId multiStringId,
  351.                                    FW_ResourceId stringId,
  352.                                    FW_CString& string)
  353. {
  354.     // ----- Open the resource file for this shared library -----
  355.     FW_CSharedLibraryResourceFile resFile;
  356.  
  357.     // ----- Read the string from the resource file -----
  358.     ::FW_LoadStringByID(resFile, multiStringId, MULTISTRINGRES, stringId, string);
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // CHelloPart::GetTextData
  363. //----------------------------------------------------------------------------------------
  364.  
  365. const FW_CString& CHelloPart::GetTextData()
  366. {
  367.     if (fIsFirstString)
  368.         return fTextData1;
  369.     else
  370.         return fTextData2;
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // CHelloPart::ClearTextData
  375. //----------------------------------------------------------------------------------------
  376.  
  377. void CHelloPart::ClearTextData()
  378. {
  379.     FW_CString255 string;
  380.     this->GetResourceString(kHelloPartStrings, kBlankString, string);
  381.     if (fIsFirstString)
  382.         fTextData1 = string;
  383.     else
  384.         fTextData2 = string;
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. // CHelloPart::SetTextData
  389. //----------------------------------------------------------------------------------------
  390.  
  391. void CHelloPart::SetTextData(const FW_CString& newText)
  392. {
  393.     if (fIsFirstString)
  394.         fTextData1 = newText;
  395.     else
  396.         fTextData2 = newText;
  397. }
  398.  
  399. //----------------------------------------------------------------------------------------
  400. // CHelloPart::GetData
  401. //----------------------------------------------------------------------------------------
  402.  
  403. void CHelloPart::GetData(Environment* ev, FW_Boolean& setting, FW_CString& string)
  404. {
  405.     setting = fIsFirstString;
  406.     string = this->GetTextData();
  407. }
  408.  
  409. //----------------------------------------------------------------------------------------
  410. // CHelloPart::SetData
  411. //----------------------------------------------------------------------------------------
  412.  
  413. void CHelloPart::SetData(Environment* ev, FW_Boolean setting, const FW_CString& string)
  414. {
  415.     fIsFirstString = setting;                // "select" the string
  416.     this->SetTextData(string);                // set the text
  417.     this->PartChanged(ev);                    // Notify the proper authorities
  418. }
  419.  
  420. //----------------------------------------------------------------------------------------
  421. // CHelloPart::ClearData
  422. //----------------------------------------------------------------------------------------
  423.  
  424. void CHelloPart::ClearData(Environment* ev, FW_Boolean setting)
  425. {
  426.     fIsFirstString = setting;                // "select" the string
  427.     this->ClearTextData();                    // then clear it
  428.     this->PartChanged(ev);                    // Notify the proper authorities
  429. }
  430.  
  431.